/**
*	Class: CastorServer
*
*	Work as an interface to handle the Castor web application.
*
*	@param url - The url of the Castor application server.
*/
function CastorServer(url){

	this.cache = new Cache(5, this.sync);
	this.username = null;
	this.password = null;
	this.isLogged = false; // TODO see if it is really necessary
	this.isAvalaible = false;

	if(typeof CastorServer.prototype._initialized == "undefined"){

		/**1
		*	Adds the recently created project into the buffer to send this info to the server.
		*
		*	@param project - the created project 
		*/		
		CastorServer.prototype.newProject = function(project){
			this.cache.newProject(project);
		};

		/**
		*	Adds the an alteration made in a project into the buffer.
		*
		*	@param project - the modified project 
		*/		
		CastorServer.prototype.alterProject = function(project){
			this.cache.alterProject(alterProject);
		};		

		/**
		*	Adds the info of a project deletion into the buffer.
		*
		*	@param projectId - the project's id which was removed
		*/		
		CastorServer.prototype.remProject = function(projectId){
			this.cache.remProject(projectId);
		};

		CastorServer.prototype.newPostIt = function(postIt, containerId, projectId){
			this.cache.newPost(postIt, containerId, projectId);
		};

		CastorServer.prototype.alterPostIt = function(postIt, projectId){
			this.cache.alterPost(postIt, projectId);
		};

		CastorServer.prototype.remPostIt = function(postId, projectId){
			this.cache.remPost(postId,projectId);
		};		
		
		CastorServer.prototype.alterContainer = function(postId, projectId, oldContainer, newContainer){
			this.cache.alterContainer(postId, projectId, oldContainer, newContainer);
		};		
		
		CastorServer.prototype.hasLoginData = function(){
			return this.username != null && this.password != null;
		};
		
		
		// ====== Server comunication methods ======
		
		/**
		*	Sends requisition and required data for registration in the Castor service.
		*/
		CastorServer.prototype.register = function(firstName, lastName, email, username, password){
			
			var awnser;
			
			$.ajax({
			
				type    : "POST",
				async   : true,
				url     : service(REGISTER),
				timeout : SERVER_TIMEOUT,
				
				data    :
				{
					'firstName' : firstName,
					'lastName'  : lastName,
					'email'     : email,
					'username'  : username,
					'password'  : password
				},
				
				success : function(msg)
				{
					awnser = msg;	
				},
				
				error : function()
				{
					
				}
				
				
			});
			
			//return awnser;
		};

		/**
		*	Send a messege to the Castor application server attempting to login with the given username an password.
		*
		*	@param loginUsername - The user's username
		*	@param loginPassword - The user's password
		*/				
		CastorServer.prototype.login = function(loginUsername, loginPassword){
			
			$.ajax({
			
				type    : "POST",
				async   : true,
				url     : service(LOGIN),
				timeout : SERVER_TIMEOUT,
				
				data    : 
				{
					username : loginUsername, //TODO check if this map works without string mapping.
					password : loginPassword
				},
				
				success : function(msg)
				{
					msg;
					
					this.username = loginUsername;
					this.password = loginPassword;			
				},
				
				error : function()
				{
				
				}				
				
			});			
			
		};
		
		/**
		*	Sends a message to the Castor server to inform that the user has dislogged.
		*/
		CastorServer.prototype.logout = function(){
			$.ajax({
				
				type      : "POST",
				async	  : false,
				url       : service(LOGOUT),
				
				success : function()
				{
					this.username = null;
					this.password = null;
					this.isLogged = false;
				},
				
				error : function()
				{
					
				}
			
			});
		};
		
		/**
		*	Sends the mintransactions buffered on the cache to the Castor server.
		*/
		CastorServer.prototype.sync = function(){
		
			var minitransactions = cache.genXML();
			var minitransactionsXml = (new DOMParser()).parseFromString(minitransactions, "text/xml");
			
			$.ajax({
			
				type        : "POST",
				async       : false,
				url         : service(SYNC),
				timeout     : SERVER_TIMEOUT,
				processData : false,
				
				data        : minitransactionsXml,
				
				success : function(msg)
				{
					//TODO check if the requisition was ok.
					this.cache.clear();
				},
				
				error : function()
				{
				
				}
							
			});			
		};
		
		// TODO implement parsers and protocol to finish this.
		CastorServer.prototype.checkout = function(){
			
			var user;
			
			$.ajax({
				
				type    : "POST",
				async   : false,
				url     : service(CHECKOUT),
				timeout : SERVER_TIMEOUT,
				
				success : function(xml)
				{
					//TODO xml parsing code goes here.
				},
				
				error : function()
				{
				
				}
				
			});
			
			return user;
			
		};

		CastorServer.prototype._initialized = true;
	}

}
